//
//  ZConnerView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/28.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation
enum ZRectCorner {
    case topLeft
    case topRight
    case bottomLeft
    case bottomRight
}

class ZConnerView: NSView {
    
    var bgColor:NSColor = NSColor.red
    var conner:[ZRectCorner] = []
    var radius:CGFloat = 5
    
    convenience init(bgColor:NSColor,conner:[ZRectCorner],radius:CGFloat) {
        self.init()
        self.bgColor = bgColor
        self.conner = conner
        self.radius = radius
        self.displayIfNeeded()
    }
    
    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        
        /*
        let context = NSGraphicsContext.current?.cgContext
        let minmidyPoint = CGPoint.init(x: dirtyRect.minX, y: dirtyRect.midY)
        
        let minxminyPoint = CGPoint.init(x: dirtyRect.minX, y: dirtyRect.minY)
        let midxminyPoint = CGPoint.init(x: dirtyRect.midX, y: dirtyRect.minY)
        let maxxminyPoint = CGPoint.init(x: dirtyRect.maxX, y: dirtyRect.minY)
        let maxxmaxxPoint = CGPoint.init(x: dirtyRect.maxX, y: dirtyRect.midY)
        let minxmaxyPoint = CGPoint.init(x: dirtyRect.minX, y: dirtyRect.maxY)
        let minxmidyPoint = CGPoint.init(x: dirtyRect.minX, y: dirtyRect.midY)
        context?.move(to: minmidyPoint)
        context?.addArc(tangent1End: minxminyPoint, tangent2End: midxminyPoint, radius: radius)
        context?.addArc(tangent1End: maxxminyPoint, tangent2End: maxxmaxxPoint, radius: 0)
        context?.addArc(tangent1End: minxmaxyPoint, tangent2End: minxmidyPoint, radius: 0)
        context?.closePath()
        self.bgColor.setFill()
        context?.fillPath()
        */
        
        /*
        let color = bgColor
        color.setFill()
        let height = self.frame.size.height
        let originalRect = NSMakeRect(0, 0, self.frame.size.width, height)
        let originbezi = NSBezierPath.init(roundedRect: originalRect, xRadius: radius, yRadius: radius)
        originbezi.fill()
        
        let coverRect = NSMakeRect(0, height-radius, self.frame.size.width, radius)
        let coverBezi = NSBezierPath.init(rect: coverRect)
        coverBezi.fill()
        */
        
        let path = NSBezierPath.init(roundedRect: self.bounds, xRadius: radius, yRadius: radius)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = path.cgPath
        self.layer?.mask = maskLayer
    
        //let gradient = NSGradient.init(starting: color_green190, ending: color_green200)
        //gradient?.draw(in: self.frame, angle: 90)
        
    }
}

public extension NSBezierPath {

    var cgPath: CGPath {
        let path = CGMutablePath()
        var points = [CGPoint](repeating: .zero, count: 3)
        for i in 0 ..< self.elementCount {
            let type = self.element(at: i, associatedPoints: &points)
            switch type {
                case .moveTo: path.move(to: points[0])
                case .lineTo: path.addLine(to: points[0])
                case .curveTo: path.addCurve(to: points[2], control1: points[0], control2: points[1])
                case .closePath: path.closeSubpath()
            }
        }
        return path
    }

}
